//
//  KTableView.m
//  Peanut
//
//  Created by Jim huang on 2018/3/12.
//  Copyright © 2018年 Jim_Peanut. All rights reserved.
//

#import "KBaseTableView.h"
#import "KBaseTableViewController.h"

@interface KBaseTableView ()

//是否有网络
@property (nonatomic ,assign) BOOL isHasNetwork;
//能否滚动
@property (nonatomic ,assign) BOOL tableScrollEnable;

@end

@implementation KBaseTableView

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.tableScrollEnable = YES;

    }
    return self;
}

-(instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style{
    self = [super initWithFrame:frame style:style];
    if (self) {
        self.tableScrollEnable = YES;
    }
    return self;
}

// 是否支持滚动
- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style scrollEnable:(BOOL)scrollEnable
{
    if (self = [super initWithFrame:frame style:style]) {
        self.tableScrollEnable = scrollEnable;
    }
    return self;
}


-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [super touchesBegan:touches withEvent:event];
    if (self.viewController) {
        [self.viewController.view endEditing:YES];
    }
}


-(BOOL)touchesShouldCancelInContentView:(UIView *)view{
    if ([view isKindOfClass:[UIControl class]]) {
        return YES;
    }
    return [super touchesShouldCancelInContentView:view];
}


-(void)setRefreshEnable:(BOOL)refreshEnable{
    _refreshEnable = refreshEnable;
    if (refreshEnable) {
        MJWeakSelf
        self.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            if ([weakSelf.refreshProtocol respondsToSelector:@selector(refresh)]) {
                [weakSelf.refreshProtocol refresh];
            }
        }];
    }else{
        if (self.mj_header) {
            self.mj_header = nil;
            [self.mj_header removeFromSuperview];
        }
    }
}
-(void)setLoadmoreEnable:(BOOL)loadmoreEnable{
    _loadmoreEnable = loadmoreEnable;
    if (loadmoreEnable) {
        MJWeakSelf
        self.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            if ([weakSelf.refreshProtocol respondsToSelector:@selector(loadmore)]) {
                [weakSelf.refreshProtocol loadmore];
            }
        }];
    }else{
        if (self.mj_footer) {
            self.mj_footer = nil;
            [self.mj_footer removeFromSuperview];
        }
        
    }
}

-(void)endRefresh{
    if (self.mj_header) {
        if (self.mj_header.isRefreshing) {
            [self.mj_header endRefreshing];
        }
    }
    if (self.mj_footer) {
        if (self.mj_footer.isRefreshing) {
            [self.mj_footer endRefreshing];
        }
    }
    [self reloadData];
}
-(void)beginRefresh{
    if (self.mj_header) {
        [self.mj_header beginRefreshing];
    }
}

-(void)endRefreshingWithNoMore{
    if (self.mj_header) {
        if (self.mj_header.isRefreshing) {
            [self.mj_header endRefreshing];
        }
    }
    
    if (self.mj_footer) {
        if (self.mj_footer.isRefreshing) {
            [self.mj_footer endRefreshingWithNoMoreData];
        }
    }
    
}


@end
